package com.evaluation.controller;

import com.evaluation.entity.AdminEntity;
import com.evaluation.entity.AdminEntityExample;
import com.evaluation.mapper.AdminEntityMapper;
import com.evaluation.utils.Layui;
import com.evaluation.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员控制器
 * @author: mty
 * @date: 2021/09/02 15:07
 * @Description:
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminEntityMapper adminEntityMapper;


    /**
     * 管理员添加操作
     * @param admin
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public Message add(@RequestBody AdminEntity admin) {
        if(StringUtils.isEmpty(admin.getUsername())){
            return Message.error("请填写用户名");
        }
        if(StringUtils.isEmpty(admin.getUserpw())){
            return Message.error("请填写密码");
        }
        AdminEntityExample example = new AdminEntityExample();
        example.or().andUsernameEqualTo(admin.getUsername());
        AdminEntity adminEntity = adminEntityMapper.selectOneByExample(example);
        if(adminEntity!=null){
            return Message.error("该用户名已存在");
        }
        if(adminEntityMapper.insert(admin)<=0){
            return Message.error("添加失败!");
        }
        return Message.success();
    }

    /**
     * 删除管理员操作
     * @param entity
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/delete", consumes = "application/json")
    public Message delete(@RequestBody AdminEntity entity) {
        if(adminEntityMapper.deleteByPrimaryKey(entity.getUserid())<=0){
                return Message.error("删除失败");
        }
        return Message.success("删除成功");
    }

    /**
     * 管理员编辑操作
     * @param admin
     * @return
     */
    @ResponseBody
    @PostMapping("/edit")
    public Message update(@RequestBody AdminEntity admin) {
        if(StringUtils.isEmpty(admin.getUsername())){
            return Message.error("请填写用户名");
        }
        if(StringUtils.isEmpty(admin.getUserpw())){
            return Message.error("请填写密码");
        }
        AdminEntityExample example = new AdminEntityExample();
        example.or().andUsernameEqualTo(admin.getUsername());
        AdminEntity adminEntity = adminEntityMapper.selectOneByExample(example);
        if(adminEntity!=null){
            if(!adminEntity.getUserid().equals(admin.getUserid())){
                return Message.error("该用户名已存在");
            }
        }
        if(adminEntityMapper.updateByPrimaryKey(admin)<=0){
            return Message.error("编辑失败,请联系管理员");
        }
        return Message.success("编辑成功");
    }

    @RequestMapping("/select")
    public Layui select(@RequestParam(required = false) String username,@RequestParam(value = "page")Integer page,
                        @RequestParam(value = "limit")Integer limit) {
        AdminEntityExample example = new AdminEntityExample();
        if (!StringUtils.isEmpty(username)){
            example.or().andUsernameLike("%"+username+"%");
        }
        example.getOrderByClause();
        Long cou = adminEntityMapper.countByExample(example);
        if(page!=1){
            page=(page-1)*10;
        }else{
            page=page-1;
        }
        return Layui.data(cou.intValue(), adminEntityMapper.selectByExamplePaging(example, page, limit));
    }

    @RequestMapping("/getAdmin")
    public AdminEntity getAdmin(@RequestParam(value="userid")Integer userid) {
        return adminEntityMapper.selectByPrimaryKey(userid);
    }

}
